﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Corp.Proj.DAL
{
    public class BaseDal<T>
        where T : class,new()
    {
        private DbContext m_Db
        {
            get
            {
                return EFDbContextFactory.GetCurrentDbContext();
            }
        }

        [Obsolete("建议仅在修改BaseDal类时使用m_Db代替此成员")]
        protected DbContext m_Ptc_Db
        {
            get
            {
                return EFDbContextFactory.GetCurrentDbContext();
            }
        }

        [Obsolete("建议使用此重载：IQueryable<T> GetQueryable(Expression<Func<T, bool>> where)")]
        public IQueryable<T> GetQueryable()
        {
            //return this.m_Ptc_Db.Set<T>().Where(x => true);
            return this.m_Db.Set<T>().Where(x => true);
        }

        public IQueryable<T> GetQueryable(Expression<Func<T, bool>> where)
        {
            return this.m_Db.Set<T>().Where(where);
        }

        [Obsolete("不建议使用，应该调用DbSession的SaveChanges方法")]
        public int SaveChanges()
        {
            return m_Db.SaveChanges();
        }

        public T Add(T entity)
        {
            return this.m_Db.Set<T>().Add(entity);
        }

        public T Remove(T entity)
        {
            return this.m_Db.Set<T>().Remove(entity);
        }

        public void Modify(T entity)
        {
            this.m_Db.Entry<T>(entity).State = EntityState.Modified;
        }       

    }
}
